﻿(function ($) {
    $.fn.buttons = function (settings) {
        for (var i = 0; i < settings.length; ++i) {
            var button = $(this).find('button').eq(i);
            if (button.length === 0) {
                $(this).append("<button />");
                button = $(this).find('button').eq(i);
            }
            var setting = settings[i];
            $(button).button(setting);
            if (setting.click)
                $(button).click(setting.click);
        }
    }

    $.fn.jqValidate = function () {
        var usage = "usage: $(form).jqValidate(url, data, callback";

        if (arguments.length !== 1 && arguments.length !== 3)
            throw usage;

        var clear = function (form) {
            $(form).find('.input-validation-error').removeClass('input-validation-error');
            $(form).find('.field-validation-error').removeClass('field-validation-error').addClass('field-validation-valid');
        }

        if (arguments.length === 1) {
            switch (arguments[0]) {
                case "clear":
                    clear(this);
                    break;

                default:
                    throw "unrecognized command";
            }
            return this;
        }

        var url = arguments[0];
        var data = arguments[1];
        var callback = arguments[2];

        var form = this;
        clear(form);

        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (data) {
                if (!data.isValid) {
                    for (var i = 0; i < data.errors.length; ++i) {
                        var control = $(form).find(':input[name=' + data.errors[i].fieldName + ']');
                        if (control.length > 0) {
                            var controlId = $(control).attr('id');
                            $(control).addClass('input-validation-error');
                            var foundValidator = false;
                            $(form).find('label[for=' + controlId + ']').each(function (index, label) {
                                if ($(label).hasClass('field-validation-error') || $(label).hasClass('field-validation-valid') || $(label).hasClass('validator')) {
                                    foundValidator = true;
                                    $(label).text(data.errors[i].errorMessage).removeClass('field-validation-valid').removeClass('validator').addClass('field-validation-error');
                                }
                            });
                            if (!foundValidator) {
                                $(control).after('<label class="field-validation-error" for="' + controlId + '">' + data.errors[i].errorMessage + '</label>');
                            }
                        }
                    }
                }
                if (callback)
                    callback(data);
                return this;
            }
        });
    }

    $.postObject = function (url, dataObject, callback) {
        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(dataObject),
            success: callback
        });
    }

    $.splitUrl = function (url) {
        var index = url.indexOf("//");
        if (index != -1)
            url = url.subString(2 + index);
        if (url[0] === "/")
            url = url.substring(1);
        return url.split("/");
    }
})(jQuery);